package com.jy.fxf_phone.view;


import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.jy.fxf_phone.R;

/**
 * A simple {@link Fragment} subclass.
 */
public class FaceFragment extends Fragment implements View.OnClickListener {
    ImageView iv_mouth, iv_eye, iv_ear;
    RelativeLayout ll_mouth, ll_eye, ll_ear;

    ImageView iv_mouth_add, iv_mouth_reduce, iv_mouth_color;
    ImageView iv_eye_add, iv_eye_reduce, iv_eye_color;
    ImageView iv_ear_add, iv_ear_reduce, iv_ear_color;
    int[] mouth_color = {R.drawable.shape_face_color_green, R.drawable.shape_face_color_red, R.drawable.shape_face_color_white};
    int[] eye_color = {R.drawable.shape_face_color_green, R.drawable.shape_face_color_red, R.drawable.shape_face_color_white};
    int[] ear_color = {R.drawable.shape_face_color_green, R.drawable.shape_face_color_red, R.drawable.shape_face_color_white};
    int mouth_color_index = 0;
    int eye_color_index = 0;
    int ear_color_index = 0;


    public FaceFragment() {
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_face, container, false);
        initViews(view);


        iv_mouth.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startAnimator(iv_mouth, ll_mouth);
            }
        });

        iv_eye.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startAnimator(iv_eye, ll_eye);
            }
        });

        iv_ear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startAnimator(iv_ear, ll_ear);
            }
        });
        return view;
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_mouth_add:
                break;
            case R.id.iv_mouth_reduce:
                break;
            case R.id.iv_mouth_color:
                iv_mouth_color.setBackground(ContextCompat.getDrawable(getContext(), mouth_color[mouth_color_index]));
                mouth_color_index++;
                if (mouth_color_index > 2) {
                    mouth_color_index = 0;
                }
                break;


            case R.id.iv_eye_add:
                break;
            case R.id.iv_eye_reduce:
                break;
            case R.id.iv_eye_color:
                iv_eye_color.setBackground(ContextCompat.getDrawable(getContext(), eye_color[eye_color_index]));
                eye_color_index++;
                if (eye_color_index > 2) {
                    eye_color_index = 0;
                }
                break;


            case R.id.iv_ear_add:
                break;
            case R.id.iv_ear_reduce:
                break;
            case R.id.iv_ear_color:
                iv_ear_color.setBackground(ContextCompat.getDrawable(getContext(), ear_color[ear_color_index]));
                ear_color_index++;
                if (ear_color_index > 2) {
                    ear_color_index = 0;
                }
                break;

        }
    }

    public void startAnimator(ImageView iv, final RelativeLayout ll) {
        AnimatorSet set = new AnimatorSet();

        ObjectAnimator upRotate = ObjectAnimator.ofFloat(iv, "rotationX", 0f, 90f);
        ObjectAnimator upTranslate = ObjectAnimator.ofFloat(iv, "translationY", 0, -iv.getHeight());
        ObjectAnimator downRotate = ObjectAnimator.ofFloat(ll, "rotationX", -90f, 0f);
        ObjectAnimator downTranslate = ObjectAnimator.ofFloat(ll, "translationY", -iv.getHeight() / 2, -iv.getHeight());

        set.play(upRotate).with(upTranslate).with(downRotate).with(downTranslate);
        set.setDuration(2000);
        set.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {
                ll.setVisibility(View.VISIBLE);
                super.onAnimationStart(animation);
            }
        });
        set.start();
    }

    private void initViews(View view) {
        iv_mouth = (ImageView) view.findViewById(R.id.iv_mouth_unselect);
        ll_mouth = (RelativeLayout) view.findViewById(R.id.ll_mouth_adjust);
        iv_mouth_add = (ImageView) view.findViewById(R.id.iv_mouth_add);
        iv_mouth_reduce = (ImageView) view.findViewById(R.id.iv_mouth_reduce);
        iv_mouth_color = (ImageView) view.findViewById(R.id.iv_mouth_color);


        iv_eye = (ImageView) view.findViewById(R.id.iv_eye_unselect);
        ll_eye = (RelativeLayout) view.findViewById(R.id.ll_eye_adjust);
        iv_eye_add = (ImageView) view.findViewById(R.id.iv_eye_add);
        iv_eye_reduce = (ImageView) view.findViewById(R.id.iv_eye_reduce);
        iv_eye_color = (ImageView) view.findViewById(R.id.iv_eye_color);

        iv_ear = (ImageView) view.findViewById(R.id.iv_ear_unselect);
        ll_ear = (RelativeLayout) view.findViewById(R.id.ll_ear_adjust);
        iv_ear_add = (ImageView) view.findViewById(R.id.iv_ear_add);
        iv_ear_reduce = (ImageView) view.findViewById(R.id.iv_ear_reduce);
        iv_ear_color = (ImageView) view.findViewById(R.id.iv_ear_color);

        iv_mouth_add.setOnClickListener(this);
        iv_mouth_reduce.setOnClickListener(this);
        iv_mouth_color.setOnClickListener(this);

        iv_eye_add.setOnClickListener(this);
        iv_eye_reduce.setOnClickListener(this);
        iv_eye_color.setOnClickListener(this);

        iv_ear_add.setOnClickListener(this);
        iv_ear_reduce.setOnClickListener(this);
        iv_ear_color.setOnClickListener(this);

    }


}
